part of '../../../repository.dart';

class _YookassaPurchasedSubscriptionTransactionDto extends Equatable
    implements _WebPurchasedSubscriptionTransactionDto, _YookassaTransactionDto {
  const _YookassaPurchasedSubscriptionTransactionDto._internal({
    required this.id,
    required this.amountDto,
    required this.offerTypeDto,
    required this.timeDto,
    required this.revocationInfoDto,
    required this.incomeAmountDto,
    required this.expirationTimeDto,
    required this.originalTransactionId,
  });

  @override
  final String id;

  @override
  final ApiMoneyAmountDto amountDto;

  @override
  final _WebPurchasedSubscriptionTransactionOfferTypeDto offerTypeDto;

  @override
  final ApiTimeDto timeDto;

  @override
  final _YookassaTransactionRevocationInfoDto? revocationInfoDto;

  @override
  final ApiMoneyAmountDto incomeAmountDto;

  @override
  final ApiTimeDto expirationTimeDto;

  @override
  final String originalTransactionId;

  @override
  List<Object?> get props {
    return [
      id,
      amountDto,
      offerTypeDto,
      timeDto,
      revocationInfoDto,
      incomeAmountDto,
      expirationTimeDto,
      originalTransactionId,
    ];
  }

  @override
  String get paymentSystem => _kWebTransactionYookassaPaymentSystemValue;

  factory _YookassaPurchasedSubscriptionTransactionDto.fromJson(Map<String, dynamic> json) {
    final amountJson = json[_kWebTransactionAmountKey] as Map<String, dynamic>;
    final amountDto = ApiMoneyAmountDto.fromJson(amountJson);

    final offerTypeString = json[_kWebPurchasedSubscriptionTransactionOfferTypeKey] as String;
    final offerTypeDto =
        _WebPurchasedSubscriptionTransactionOfferTypeDto.fromString(offerTypeString);

    final transactionTimeString = json[_kWebTransactionTimeKey] as String;
    final transactionTimeDto = ApiTimeDto.fromString(transactionTimeString);

    final incomeAmountJson = json[_kYookassaTransactionIncomeAmountKey] as Map<String, dynamic>;
    final incomeAmountDto = ApiMoneyAmountDto.fromJson(incomeAmountJson);

    final revocationInfoJson =
        json[_kYookassaTransactionRevocationInfoKey] as Map<String, dynamic>?;

    _YookassaTransactionRevocationInfoDto? revocationInfoDto;

    if (revocationInfoJson != null) {
      revocationInfoDto = _YookassaTransactionRevocationInfoDto.fromJson(revocationInfoJson);
    }

    final expirationTimeString =
        json[_kWebPurchasedSubscriptionTransactionExpirationTimeKey] as String;
    final expirationTimeDto = ApiTimeDto.fromString(expirationTimeString);

    return _YookassaPurchasedSubscriptionTransactionDto._internal(
      id: json[_kWebTransactionIdKey],
      amountDto: amountDto,
      offerTypeDto: offerTypeDto,
      timeDto: transactionTimeDto,
      revocationInfoDto: revocationInfoDto,
      incomeAmountDto: incomeAmountDto,
      originalTransactionId: json[_kWebPurchasedSubscriptionTransactionOriginalTransactionIdKey],
      expirationTimeDto: expirationTimeDto,
    );
  }

  @override
  YookassaPurchasedSubscriptionTransaction toEntity() {
    return YookassaPurchasedSubscriptionTransaction(
      id: id,
      time: timeDto.toEntity(),
      amount: amountDto.toEntity(),
      offerType: offerTypeDto.toEntity(),
      revocationInfo: revocationInfoDto?.toEntity(),
      incomeAmount: incomeAmountDto.toEntity(),
      expirationTime: expirationTimeDto.toEntity(),
      originalTransactionId: originalTransactionId,
    );
  }
}
